# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>

pkgbase=harfbuzz
pkgname=(
  harfbuzz
  harfbuzz-cairo
  harfbuzz-icu
  harfbuzz-utils
  harfbuzz-docs
)
pkgver=7.3.0
pkgrel=1
pkgdesc="OpenType text shaping engine"
url="https://www.freedesktop.org/wiki/Software/HarfBuzz"
arch=(x86_64)
license=(MIT)
makedepends=(
  cairo
  chafa
  freetype2
  git
  glib2
  gobject-introspection
  graphite
  gtk-doc
  icu
  meson
  python
  ragel
)
checkdepends=(
  python-fonttools
  python-setuptools
)
_commit=4584bcdc326564829d3cee3572386c90e4fd1974  # tags/7.3.0^0
source=("git+https://github.com/harfbuzz/harfbuzz#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd harfbuzz
  git describe --tags | sed 's/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd harfbuzz
}

build() {
  local meson_options=(
    -D graphite2=enabled
  )

  # Harfbuzz wants no exceptions
  CFLAGS="${CFLAGS/-fexceptions/}"
  CXXFLAGS="${CXXFLAGS/-fexceptions/}"

  arch-meson harfbuzz build "${meson_options[@]}"
  meson compile -C build
}

check() {
  mkdir -p tmp
  TMPDIR="$PWD/tmp" meson test -C build --print-errorlogs
  rm -r tmp
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_harfbuzz() {
  depends=(
    libfreetype.so
    libglib-2.0.so
    libgobject-2.0.so
    libgraphite2.so
  )
  optdepends=('harfbuzz-utils: utilities')
  provides=(libharfbuzz{,-subset,-gobject}.so)

  meson install -C build --destdir "$pkgdir"

  ( cd "$pkgdir"

    _pick hb-cairo usr/lib/libharfbuzz-cairo*
    _pick hb-cairo usr/lib/pkgconfig/harfbuzz-cairo.pc
    _pick hb-cairo usr/include/harfbuzz/hb-cairo.h

    _pick hb-icu usr/lib/libharfbuzz-icu*
    _pick hb-icu usr/lib/pkgconfig/harfbuzz-icu.pc
    _pick hb-icu usr/include/harfbuzz/hb-icu.h

    _pick hb-utils usr/bin

    _pick hb-docs usr/share/gtk-doc
  )

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 harfbuzz/COPYING
}

package_harfbuzz-cairo() {
  pkgdesc+=" - Cairo integration"
  depends=(
    libcairo.so
    libharfbuzz.so
  )
  provides=(libharfbuzz-cairo.so)

  mv hb-cairo/* "$pkgdir"

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 harfbuzz/COPYING
}

package_harfbuzz-icu() {
  pkgdesc+=" - ICU integration"
  depends=(
    libharfbuzz.so
    libicuuc.so
  )
  provides=(libharfbuzz-icu.so)

  mv hb-icu/* "$pkgdir"

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 harfbuzz/COPYING
}

package_harfbuzz-utils() {
  pkgdesc+=" - Utilities"
  depends=(
    harfbuzz
    harfbuzz-cairo
    libcairo.so
    libchafa.so
    libfreetype.so
    libglib-2.0.so
    libgobject-2.0.so
    libharfbuzz-cairo.so
    libharfbuzz-gobject.so
    libharfbuzz-subset.so
    libharfbuzz.so
  )

  mv hb-utils/* "$pkgdir"

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 harfbuzz/COPYING
}

package_harfbuzz-docs() {
  pkgdesc+=" - Documentation"
  depends=()

  mv hb-docs/* "$pkgdir"

  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 harfbuzz/COPYING
}

# vim:set sw=2 sts=-1 et:
